import java.util.Arrays;

public class Mylist implements List{

    public static int val;//实例对象的值
    public static int pos;//实例对象的位置
    public static int Usedsize;//用来记录数组的有效值
    public int[] List;
    public static final int number = 5;//顺序表初始容量

    public Mylist() {
        this.List = new int[number];
    }
    private Boolean IsEmpty(){
        return Usedsize == 0;
    }//检查顺序表是否为空
    private  Boolean Isfull(){
        return List.length == Usedsize;
    }//检查顺序表是否满了
    private void grow(){
        List = Arrays.copyOf(List,2*List.length);
    }//进行扩容操作（二倍扩容）
    private Boolean Checkpos(int pos){
        if (pos < 0 || pos > Usedsize){
            return false;
        }
        return true;
    }//检查插入数的pos位置是否合理

    @Override
    public void add(int data) {
        if (Isfull()){
            grow();
            return;
        }
        List[Usedsize] = data;
        Usedsize++;
        return;
    }//新增元素，默认在最后面增

    @Override
    public void add(int pos, int data) {
        if(Isfull()){
            grow();
        }
        if (pos == Usedsize){
            add(data);
            Usedsize++;
            return;
        }
        if (Checkpos(pos)){
            int tem = 0;
            for (int i = Usedsize; i > pos; i--) {
                List[i] = List[i - 1];
            }
            List[pos] = data;
            Usedsize++;
            return;
        }else {
            System.out.println("pos 位置有问题");
        }

    }//在pos位置新增元素

    @Override
    public boolean contains(int toFind) {
        for (int i = 0; i < Usedsize; i++) {
            if (List[i] == toFind){
                return true;
            }
        }
        return false;
    }

    @Override
    public int indexOf(int toFind) {
        if (contains(toFind)){
            for (int i = 0; i < Usedsize; i++) {
                if (List[i] ==toFind){
                    System.out.print(i +" ");
                }
            }
        }
        return -1;
    }

    @Override
    public int get(int pos) {
        if (IsEmpty()){
            throw new EmptuException("顺序表为空");
        }
        if (pos < 0 || pos >= Usedsize){
            System.out.println("pos位置不合理");
        }
        return List[pos];
    }

    @Override
    public void set(int pos, int value) {
        if (IsEmpty()){
            throw new EmptuException("顺序表为空");
        }
        if (pos < 0 || pos >= Usedsize){
            System.out.println("pos位置不合理");
        }
        List[pos] = value;
    }

    @Override
    public void remove(int toRemove) {
        if (contains(toRemove)) {
            for (int i = 0; i < Usedsize; i++) {
                if (List[i] == toRemove) {
                    for (int j = i; j < Usedsize - 1; j++) {
                        List[i] = List[i + 1];
                        List[i + 1] = 0;
                    }
                }
            }
        }
        Usedsize--;
    }

    @Override
    public int size() {
        return Usedsize;
    }

    @Override
    public void clear() {
        Usedsize = 0;
    }

    @Override
    public void display() {
        for (int i = 0; i < List.length; i++) {
            System.out.print(List[i]+" ");
        }
    }
}

